<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>openGauss Blog  | 兼容多版本Python的第三方库编译说明</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link rel="shortcut icon" href="https://xzx666.gitee.io/lookeng/img/favicon.ico" type="image/x-icon" />
    <link rel="apple-touch-icon" href="https://xzx666.gitee.io/lookeng/img/apple-touch-icon.png" />

    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/commen.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/blog.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/mobile.css?t=1607593672000" />
    <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/pagination.css?t=1607593672000">
    
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?34e9f585f29581007941aa1698181871";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
</head>
<body>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>






<div class="container">
    <div class="columns blog-detail">
        <div class="post_detail">
            <div class="is-child box">
                <div class="breadCrumb"><a href="\zh\">博客/</a></div>
                <div class="blog-detail-header">
                    <h1>兼容多版本Python的第三方库编译说明</h1>
                    <div class="blog-detail-prop">
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-date.png">
                        <span class="article_right_date">2020-11-09</span>
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-author.png">
                        <span class="article_right_author">Ricardo.Cui</span>
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-view.png">
                        <span id="/zh/post/ricardo/support-python3.x-version/" class="leancloud_visitors" data-flag-title="兼容多版本Python的第三方库编译说明">
                            <i class="leancloud-visitors-count"></i>                     
                        </span>
                    </div>
                    <div class="blog-detail-tags">
                        <img src="https://xzx666.gitee.io/lookeng/img/icon-tag.png">
                        
                        <a href="https://xzx666.gitee.io/lookeng/zh/tags/%E5%85%BC%E5%AE%B9%E5%A4%9A%E7%89%88%E6%9C%ACpython%E7%9A%84%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93%E7%BC%96%E8%AF%91%E8%AF%B4%E6%98%8E">兼容多版本Python的第三方库编译说明</a>
                        
                    </div>
                </div>
                <div class="content blog-content">
                    

<h3 id="概述">概述</h3>

<p>本文描述了如何在CentOS系统上编译出支持多版本python环境的openGauss-third_party第三方库。</p>

<p><strong>硬件规格：</strong><br />
CPU:    Intel&reg; Xeon&reg; Gold 6266C CPU @ 3.00GHz 7核<br />
内存: 32G</p>

<p><strong>软件规格：</strong><br />
操作系统:   CentOS Linux release 7.6.1810 (Core)<br />
数据库: openGauss 1.0.1<br />
Python版本：3.7.7</p>

<hr />

<h3 id="编译步骤">编译步骤</h3>

<p>若希望编译出的支持多版本安装的第三方库，只需在所需环境下单独编译CFFI模组，并将其放入openGauss-third_party_binartlibs//dependency/centos7.6_x86_64/install_tools目录下即可，下面为详细过程。</p>

<p><strong>1.环境准备</strong></p>

<p>首先确定所用系统为CentOS x86_64, 接着安装编译所需组件：<br />
&gt;openSSL<br />
openSSL-devel<br />
libaio-devel<br />
ncurses-devel<br />
pam-devel<br />
libffi-devel<br />
python3-devel<br />
libtool</p>

<p>建议采用yum install *** 方式安装。</p>

<p><strong>2.编译安装Python3.X.X</strong><br />
官网下载所需的Python3.X.X源代码并解压。进入解压出来的python目录用运行如下命令：<br />
&gt;<code>vi Modules/Setup.dist</code></p>

<p>搜索SSL并且取消注释如下代码：</p>

<blockquote>
<p><code># Socket module helper for socket(2)</code><br />
<code>_socket socketmodule.c</code></p>

<p><code># Socket module helper for SSL support; you must comment out the other</code><br />
<code># socket line above, and possibly edit the SSL variable:</code><br />
<code>#SSL=/usr/local/ssl</code><br />
<code>_ssl _ssl.c</code><br />
<code>-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl</code><br />
<code>-L$(SSL)/lib -lssl -lcrypto</code></p>
</blockquote>

<p>如果SSL不是在默认路径，则需修改SSL路径为安装目录。</p>

<p>编译安装python到指定目录：
&gt;<code>./configure --prefix=/usr/local/python3.x.x/ --enable-shared</code><br />
<code>make &amp;&amp; make install</code></p>

<p>安装成功后运行<code>python -V</code>命令查看python版本是否为所需版本，若不是请去/usr/bin目录修改python软链接使其指向正确的python安装目录。</p>

<p><strong>3.编译第三方库</strong><br />
从社区下载的第三方编译源码openGauss-third_party,
进入openGauss-third_party/dependency/build，打开build_dependency.sh脚本，注释掉不需要编译的第三方组件：<br />
&gt;&hellip;<br />
<code>function build_pylib()</code>
<code>{</code>
<code>#    build_item six</code><br />
<code>#    build_item pycparser</code><br />
<code>build_item cffi</code><br />
<code>#    build_item bcrypt</code><br />
<code>#    build_item idna</code><br />
<code>#    build_item ipaddress</code><br />
<code>#    build_item netifaces</code><br />
<code>#    build_item pynacl</code><br />
<code>#    build_item asn1crypto</code><br />
<code>#    build_item cryptography</code><br />
<code>#    build_item pyOpenSSL</code><br />
<code>#    build_item paramiko</code><br />
<code>#    build_item psutil</code><br />
<code>#    build_item pyasn1</code><br />
<code>if [ -d $BUILD_SCRIPT_PATH/../install_comm ];then</code><br />
<code>rm -rf $BUILD_SCRIPT_PATH/../install_comm</code><br />
<code>fi</code><br />
<code>}</code><br />
&hellip;</p>

<p>注释掉Main函数中的<br />
&gt;&hellip;<br />
<code>#    build_first</code><br />
<code>#    build_second</code><br />
<code>build_pylib</code><br />
&hellip;</p>

<p>再进入openGauss-third_party/build文件夹下，打开build_all.sh脚本,在最下方注释掉以下几行：
&gt;<code>#    build_item buildtools</code><br />
    <code>build_item dependency</code><br />
<code>#    build_item platform</code></p>

<p>在build目录下运行脚本：<br />
&gt;<code>cd ..</code><br />
<code>sh build_all.sh</code></p>

<p>binarylibs会生成到openGauss-third_party同级目录下，名为binarylibs。如果部分组件构建失败，可以检查日志。
将binarylibs拷贝至openGauss-third_party_binarylibs：
&gt;<code>\cp -r binarylibs/ openGauss-third_party_binarylibs/</code></p>

<p>如果希望在binarylibs中支持多个python版本，则需在多个版本python中重复以上步骤，编译对应版本的cffi，并将编译出的文件，放入openGauss-third_party_binarylibs/dependency/centos7.6_x86_64/install_tools目录下</p>

<pre><code>drwxr-xr-x 2 root root   4096 Nov  3 15:24 bcrypt  
drwxr-xr-x 2 root root   4096 Nov  3 15:24 cffi  
-rw-r--r-- 1 root root    289 Nov  3 15:24 _cffi_backend.py
-rwxr-xr-x 1 root root 189816 Nov  3 15:24 _cffi_backend.so_UCS2_3.6
-rwxr-xr-x 1 root root 189816 Nov  4 09:21 _cffi_backend.so_UCS2_3.7
-rwxr-xr-x 1 root root 189816 Nov  3 15:24 _cffi_backend.so_UCS4_3.6
-rwxr-xr-x 1 root root 189816 Nov  4 09:21 _cffi_backend.so_UCS4_3.7
drwxr-xr-x 4 root root   4096 Nov  3 15:26 cryptography
drwxr-xr-x 4 root root   4096 Nov  3 15:26 nacl
drwxr-xr-x 2 root root   4096 Nov  3 15:25 netifaces
drwxr-xr-x 2 root root   4096 Nov  3 15:26 psutil
</code></pre>

                </div>
            </div>
            <div class="box" style="margin-top:15px;">
                <div style="font-size:14px;color:gray"><strong>【免责声明】</strong>本文仅代表作者本人观点，与本网站无关。本网站对文中陈述、观点判断保持中立，不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文仅供读者参考，由此产生的所有法律责任均由读者本人承担。</div>
            </div>
            <div class="post-comment">
                
                  
  <link rel="stylesheet" href="https://xzx666.gitee.io/lookeng/css/custom.css" />
  <div id="vcomments" lang="zh-cn" mess="说点什么吧..."></div>

  <script type="text/javascript">
    const placeholder = document.getElementById("vcomments").getAttribute("mess"),
          lang = document.getElementById("vcomments").getAttribute("lang");
    const  langs = lang == "zh-cn" ? "zh-cn" : "en";
    new Valine({
        el: '#vcomments' ,
        appId: '6wfgavgIRqmpC3hjHqQVtFWF-gzGzoHsz',
        appKey: 'QRqrBDBB0p0YhrGe9IJ169ip',
        avatar:'mm', 
        placeholder: placeholder,
        visitor: true,
		    meta: ['nick','mail','link'],
        lang: langs
    });
  </script>
            </div>
        </div>
    </div>
</div>
<script>
    $(function (){
        var query = new AV.Query('Counter');
        query.equalTo('url', decodeURIComponent('\/zh\/post\/ricardo\/support-python3.x-version\/'));
        query.find().then(function (data) {
            if(data.length && (data[0].attributes.author === 'openGauss')){
                var counter = AV.Object.createWithoutData('Counter', data[0].id);
                counter.set('author', 'Ricardo.Cui');
                counter.save();
            }
        })
    })
</script>







<input id="iframeUrl" type="text" style="display: none;" value=https://xzx666.gitee.io/>




<script defer src="https://xzx666.gitee.io/lookengjs/all.js"></script>
<script src="https://xzx666.gitee.io/lookengjs/flexible.js"></script>
<script>
  const hash = window.location.search,
        pageurl = window.location.href;
  var langss = document.querySelector("html").lang === "zh-cn" ? "zh" : "en";
  console.log("hash:",hash,"pageurl:",pageurl,"ttr",pageurl.split(langss + "/")[1])

  if(!document.getElementById("vcomments")) {
    new Valine({
        appId: '6wfgavgIRqmpC3hjHqQVtFWF-gzGzoHsz',
        appKey: 'QRqrBDBB0p0YhrGe9IJ169ip',
        avatar:'mm',
        visitor: true,
		    meta: ['nick','mail','link']
    });
  }
 
  
    


    const url = document.getElementById("iframeUrl").value;

    function observe (el, options, callback) {
      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
      var observer = new MutationObserver(callback)
      observer.observe(el, options)
    }

    var options = {
        childList: true,
        subtree: true,
        characterData: true
      }
    observe(document.body, options, (records, instance) => {
    const height = document.body.scrollHeight;
    parent.postMessage(height,url);
    })
    if(pageurl.split(langss + "/")[1]) parent.postMessage(pageurl.split(langss + "/")[1],url);
    

    if(document.querySelector("#notFound")) parent.postMessage("我404了",url);

</script>
</body>
</html>
